package net.caixiaomi.info.Lottery.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;

/**
 * Created by suntian on 2018/8/16.
 */

public class TrendSelectedView extends LinearLayout implements View.OnClickListener{
    private LinearLayout llt_title;
    private RelativeLayout trend_select_group0,
            trend_select_group1,
            trend_select_group2,
            trend_select_group3,
            trend_select_group4;

    private View cursor;

    private int currentIndex;//当前页卡编号
    public int subTitleWidth;//子标题宽度，cursor平移距离
    private int mLineWidth; //每个条目的宽度
    private int mSpaceWidth;    //每个条目文字的间隙

    private int mScreenWidth;
    private TextView mContentTv0;
    private TextView mContentTv1;
    private TextView mContentTv2;
    private TextView mContentTv3;
    private TextView mContentTv4;


    private MatchingSelectCallBack mCallBack;

    public TrendSelectedView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mScreenWidth= CommonApp.mScreenWidth;
        View view=View.inflate(context, R.layout.trend_selected_layout,null);
        addView(view);

        llt_title = (LinearLayout) view.findViewById(R.id.llt_title);
        mContentTv0= (TextView) view.findViewById(R.id.trend_select_tv0);
        mContentTv1= (TextView) view.findViewById(R.id.trend_select_tv1);
        mContentTv2= (TextView) view.findViewById(R.id.trend_select_tv2);
        mContentTv3= (TextView) view.findViewById(R.id.trend_select_tv3);
        mContentTv4= (TextView) view.findViewById(R.id.trend_select_tv4);

        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) llt_title.getLayoutParams();
        params.width = mScreenWidth;
        llt_title.setLayoutParams(params);

        trend_select_group0 = (RelativeLayout) view.findViewById(R.id.trend_select_group0);
        trend_select_group1 = (RelativeLayout) view.findViewById(R.id.trend_select_group1);
        trend_select_group2 = (RelativeLayout) view.findViewById(R.id.trend_select_group2);
        trend_select_group3 = (RelativeLayout) view.findViewById(R.id.trend_select_group3);
        trend_select_group4 = (RelativeLayout) view.findViewById(R.id.trend_select_group4);
        cursor = findViewById(R.id.cursor);

        trend_select_group0.setOnClickListener(this);
        trend_select_group1.setOnClickListener(this);
        trend_select_group2.setOnClickListener(this);
        trend_select_group3.setOnClickListener(this);
        trend_select_group4.setOnClickListener(this);

        subTitleWidth=mScreenWidth/5;

        post(new Runnable() {
            @Override
            public void run() {
                initTabLine();
                setPageSelected(0);
            }
        });
    }

    public void setCallBack(MatchingSelectCallBack callBack){
        mCallBack=callBack;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.trend_select_group0:
                mCallBack.matchingBarSelected(0);
                break;
            case R.id.trend_select_group1:
                mCallBack.matchingBarSelected(1);
                break;
            case R.id.trend_select_group2:
                mCallBack.matchingBarSelected(2);
                break;
            case R.id.trend_select_group3:
                mCallBack.matchingBarSelected(3);
                break;
            case R.id.trend_select_group4:
                mCallBack.matchingBarSelected(4);
                break;
        }
    }

    public void setPageSelected(int position){
        Animation animation = new TranslateAnimation(
                currentIndex * subTitleWidth+mSpaceWidth,  //from X
                position * subTitleWidth+mSpaceWidth,      //to X
                0, 0
        );

        //cursor滑动
        currentIndex = position;
        animation.setFillAfter(true);//动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setInterpolator(new DecelerateInterpolator());//减速动画
        animation.setDuration(300);//动画持续时间0.3秒
        cursor.startAnimation(animation);
        llt_title.getChildAt(position).requestFocus();

        //title的text变色
        setMatchingSelect(position);
    }

    /**
     * 初始化tab下划线
     * 实现步骤：
     * 1、获取屏幕大小 比例设置宽度
     * 2、然后有几个tab就分几份  移动时改变外边距参数
     */
    private void initTabLine() {
        mLineWidth = mScreenWidth / 5;
        mSpaceWidth=(mLineWidth-mContentTv0.getMeasuredWidth())/2;

        LinearLayout.LayoutParams mLineparams = (LinearLayout.LayoutParams) cursor.getLayoutParams();
        mLineparams.width = mContentTv0.getMeasuredWidth();
        cursor.setLayoutParams(mLineparams);
    }


    /**
     * 选择比赛栏目
     */
    private void setMatchingSelect(int type){

        clearMatchingSelect();

        switch (type){
            case 0: //未结束
                mContentTv0.setTextColor(getResources().getColor(R.color.orange_primary));
                break;
            case 1: //未结束
                mContentTv1.setTextColor(getResources().getColor(R.color.orange_primary));
                break;
            case 2: //未结束
                mContentTv2.setTextColor(getResources().getColor(R.color.orange_primary));
                break;
            case 3: //未结束
                mContentTv3.setTextColor(getResources().getColor(R.color.orange_primary));
                break;
            case 4: //未结束
                mContentTv4.setTextColor(getResources().getColor(R.color.orange_primary));
                break;
        }
    }

    private void clearMatchingSelect(){
        mContentTv0.setTextColor(getResources().getColor(R.color.third_text));
        mContentTv1.setTextColor(getResources().getColor(R.color.third_text));
        mContentTv2.setTextColor(getResources().getColor(R.color.third_text));
        mContentTv3.setTextColor(getResources().getColor(R.color.third_text));
        mContentTv4.setTextColor(getResources().getColor(R.color.third_text));
    }
}
